using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Controls;
using SProject.Paramter;

namespace ProjectView.ProjectControls
{
	public class UCBaseOption : UserControl
	{
		public static readonly RoutedEvent OnParamterChanngeRoutedEvent = EventManager.RegisterRoutedEvent("OnParamterChanngeRoutedEvent", RoutingStrategy.Bubble, typeof(EventHandler<RoutedEventArgs>), typeof(UCBaseOption));

		private List<int> tubeNoList = new List<int>();

		public event RoutedEventHandler OnParamterChannge
		{
			add
			{
				AddHandler(OnParamterChanngeRoutedEvent, value);
			}
			remove
			{
				RemoveHandler(OnParamterChanngeRoutedEvent, value);
			}
		}

		public void RaiseOnParamterChanngeEvent()
		{
			RaiseEvent(new RoutedEventArgs
			{
				RoutedEvent = OnParamterChanngeRoutedEvent
			});
		}

		public UCBaseOption()
		{
			base.SourceUpdated += UCBaseOption_SourceUpdated;
			AddHandler(DataGridEx.OnItemUpdateRoutedEvent, new RoutedEventHandler(OnItemUpdate_RoutedEvent), true);
		}

		private void OnItemUpdate_RoutedEvent(object sender, RoutedEventArgs e)
		{
			RaiseOnParamterChanngeEvent();
		}

		private void UCBaseOption_SourceUpdated(object sender, DataTransferEventArgs e)
		{
			RaiseOnParamterChanngeEvent();
		}

		protected virtual void grid_LoadingRow(object sender, DataGridRowEventArgs e)
		{
			if (e.Row.GetIndex() == 0)
			{
				tubeNoList.Clear();
			}
			BasicOptionItem basicOptionItem = e.Row.DataContext as BasicOptionItem;
			if (basicOptionItem != null && !tubeNoList.Contains(basicOptionItem.TubeNo))
			{
				tubeNoList.Add(basicOptionItem.TubeNo);
				if (tubeNoList.Count > 1)
				{
					e.Row.SetResourceReference(FrameworkElement.StyleProperty, "DataGridRowStyleTubeSplit");
				}
			}
		}
	}
}
